<?php
/**
 * Created by JetBrains PhpStorm.
 * User: shaoshuai
 * Date: 12-7-17
 * Time: 下午9:19
 * To change this template use File | Settings | File Templates.
 */
include 'boot.php';
session_start();

Functions::log($_SESSION);

$myName = $_SESSION['username'];
$myRole = $_SESSION['role'];

$room = Room::fromId($_SESSION['room']);
$status = $room->fire($myRole, intval($_GET['x']), intval($_GET['y']));
$room->save();
if ($room->isOver()) {
    $winner = $room->getWinner();
    if ($winner == 'owner') {
        EventDispatcher::dispatch($room->getOwner(), new WinEvent());
        EventDispatcher::dispatch($room->getGuest(), new LostEvent());
    } else {
        EventDispatcher::dispatch($room->getGuest(), new WinEvent());
        EventDispatcher::dispatch($room->getOwner(), new LostEvent());
    }
}

$output = array(
    'success' => true,
    'data' => array(
        'status' => $status,
        'x' => intval($_GET['x']),
        'y' => intval($_GET['y']),
    ),
);
echo json_encode($output);